package com.itheima.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class ExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                         Object o, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
      /*  modelAndView.addObject("msg","出错了");
        modelAndView.setViewName("error.jsp");*/

       if(e instanceof NullPointerException){
            modelAndView.addObject("msg","空指针异常");
        }else if (e instanceof ArithmeticException){
            modelAndView.addObject("msg","算术异常");
        }else {
            modelAndView.addObject("msg","其他异常");
        }

        return modelAndView;
    }
}
